package com.example.administrator.demoa;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LikeActivity extends AppCompatActivity {
    private RecyclerView rv_like;
    private List<Data> dataList = new ArrayList<>();
    private NewsListAdapter adapter;
    private LikeReceiver likeReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_like);

        setTitle("我的收藏");

        rv_like = findViewById(R.id.rv_like);
        adapter = new NewsListAdapter(dataList);
        rv_like.setAdapter(adapter);
        rv_like.setLayoutManager(new LinearLayoutManager(LikeActivity.this));

        initReceiver();

        //通过查询获取收藏的数据并显示
        getLikeData();
    }

    //通过发送广播来提醒是否收藏，以便及时更新数据
    private void initReceiver() {
        //注册广播
        likeReceiver = new LikeReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("LIKE_CHANGED");
        registerReceiver(likeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁广播
        unregisterReceiver(likeReceiver);
    }

    private void getLikeData() {
        BmobQuery<Like> likeBmobQuery = new BmobQuery<>();
        likeBmobQuery.addWhereEqualTo("userId", BmobUser.getCurrentUser(BmobUser.class).getObjectId());
        likeBmobQuery.addWhereEqualTo("isLike", true);
        likeBmobQuery.findObjects(new FindListener<Like>() {
            @Override
            public void done(List<Like> list, BmobException e) {
                if(e == null){
                    //先清空列表
                    dataList.clear();
                    if(list != null && list.size() > 0){
                        for(Like like: list){
                            String newsJson = like.getNewsJson();
                            //将newsJson的String类型转换为Data类型
                            Data data = new Gson().fromJson(newsJson, Data.class);
                            dataList.add(data);
                        }
                    }
                    //刷新数据（改变数据的显示）
                    adapter.changeData(dataList);
                }
            }
        });
    }

    class LikeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //更新收藏数据
            if(intent.getAction().equals("LIKE_CHANGED")){
                getLikeData();
            }
        }
    }
}
